#!/bin/gawk -f

BEGIN {
    err = 0
    prev = 0
}

{
# remove chain identifier if present
    if ( NF == 15 && $1 ~ /^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]$/ ) {
	sub( /^ *[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/, "" )
    }
    if ( NF != 14 ) {
	print "ERROR in NF"
        err = 1
	exit( 1 )
    }
#    if ( prev != $1 - 1 && NR != 1 && $1 != 1 ) {
#        err = 1
#	print "ERROR in res.nr."
#	print $0
#	exit( 1 )
#    }
    prev = $1
    for ( i=3; i<=14; i++ ) {
	if ( $i != "?" ) {
	    sum[i] += $i
	    cnt[i]++
	}
    }
}

END {
    if ( !err) {
    for ( i=3; i<=14; i++ ) {
	if ( cnt[i] == 0 ) {
	    sum[i] = 9999
	    cnt[i] = 1
	}
    }
    print ""
#    print "$ DATA  PCEDT-RANGE-03-20"
    print "$ DATA  PCEDT-NORANGE"
    print "$ LIST #RECORDS 12 "
    print "cv-phi       "sum[3]/cnt[3]
    print "cv-psi       "sum[4]/cnt[4]
    print "cv-chi1      "sum[5]/cnt[5]
    print "cv-chi2      "sum[6]/cnt[6]
    print "cv-phi-psi   "sum[7]/cnt[7]
    print "cv-chi1-chi2 "sum[8]/cnt[8]
    print "dev-main     "sum[9]/cnt[9]
    print "dev-side     "sum[10]/cnt[10]
    print "gf-phi-psi   "sum[11]/cnt[11]
    print "gf-chi1-chi2 "sum[12]/cnt[12]
    print "gf-chi1      "sum[13]/cnt[13]
    print "gf-all       "sum[14]/cnt[14]
    print "$ END"
    print ""
    }
}
